package com.frame.easy.modular.activiti.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.frame.easy.base.controller.BaseController;
import com.frame.easy.modular.activiti.model.Model;
import com.frame.easy.modular.activiti.service.ModelService;
import com.frame.easy.result.Tips;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Activiti 流程模型
 *
 * @author tengchong
 * @date 2019-07-03
 */
@Controller
@RequestMapping("/auth/activiti/model")
public class ModelController extends BaseController {

    /**
     * view 路径
     */
    private static final String PREFIX = "modular/activiti/model/";

    @Autowired
    private ModelService service;

    /**
     * 列表
     *
     * @return String
     */
    @RequestMapping("list")
    public String list() {
        logger.debug("/auth/activiti/model/list");
        return PREFIX + "list";
    }

    /**
     * 列表
     *
     * @param object 查询条件
     * @return Tips
     */
    @RequestMapping("select")
    @ResponseBody
    @RequiresPermissions("activiti:model:select")
    public Tips select(@RequestBody(required = false) Model object) {
        logger.debug("/auth/activiti/model/select");
        return Tips.getSuccessTips(service.select(object));
    }

    /**
     * 详情
     *
     * @param id id
     * @return String
     */
    @RequestMapping("/input/{id}")
    @RequiresPermissions("activiti:model:select")
    public String input(org.springframework.ui.Model model, @PathVariable("id") String id) {
        logger.debug("/auth/activiti/model/input/" + id);
        model.addAttribute("object", service.input(id));
        return PREFIX + "input";
    }

    /**
     * 详情
     *
     * @param id id
     * @return String
     */
    @RequestMapping("/model/{id}/json")
    @RequiresPermissions("activiti:model:select")
    @ResponseBody
    public ObjectNode getModel(@PathVariable("id") String id) {
        logger.debug("/auth/activiti/model/" + id + "/json");
        return service.getModel(id);
    }


    @GetMapping("/edit/{modelId}")
    public String edit(@PathVariable("modelId") String modelId) {
        return REDIRECT + "/static/modular/activiti/modeler.html?modelId=" + modelId;
    }

    /**
     * 新增
     *
     * @return String
     */
    @RequestMapping("/add")
    @RequiresPermissions("activiti:model:add")
    public String add(org.springframework.ui.Model model) {
        logger.debug("/auth/activiti/model/add");
        model.addAttribute("object", service.add());
        return PREFIX + "input";
    }

    /**
     * 删除
     *
     * @param ids 数据ids
     * @return Tips
     */
    @RequestMapping("/delete/{ids}")
    @ResponseBody
    @RequiresPermissions("activiti:model:delete")
    public Tips delete(@PathVariable("ids") String ids) {
        logger.debug("/auth/activiti/model/delete/" + ids);
        return Tips.getSuccessTips(service.delete(ids));
    }

    /**
     * 保存
     *
     * @param object 表单内容
     * @return Tips
     */
    @RequestMapping("/save/data")
    @ResponseBody
    @RequiresPermissions("activiti:model:save")
    public Tips saveData(Model object) {
        logger.debug("/auth/activiti/model/save/data");
        return Tips.getSuccessTips(service.saveData(object));
    }

    @RequestMapping("/model/{id}/save")
    @ResponseBody
    public void saveModel(@PathVariable("id") String id, String name,
                          String description,
                          @RequestParam("json_xml") String jsonXml,
                          @RequestParam("svg_xml") String svgXml) {
        service.saveModel(id, name, description, jsonXml, svgXml);
    }

    /**
     * 读取stencilset.json文件
     *
     * @return 文件内容
     */
    @GetMapping("/editor/stencilset")
    @ResponseBody
    public String getStencilset() {
        return service.getStencilset();
    }

    /**
     * 部署流程
     *
     * @param id 模型id
     * @return true/false
     */
    @RequestMapping("/deployment/process/{id}")
    @ResponseBody
    @RequiresPermissions("activiti:model:deployment")
    public Tips deploymentProcess(@PathVariable("id") String id) {
        return Tips.getSuccessTips(service.deploymentProcess(id));
    }

    /**
     * 导出模型
     *
     * @param id 模型id
     */
    @RequestMapping("/export/{id}")
    @RequiresPermissions("activiti:model:export")
    public String export(@PathVariable("id") String id) {
        String sysDownloadId = service.export(id);
        return REDIRECT + "/download/" + sysDownloadId;
    }

    /**
     * 根据模型标识查询流程定义ID
     *
     * @param key 模型标识
     * @return Tips 流程定义ID
     */
    @RequestMapping("/select/process/definition/id/{key}")
    @ResponseBody
    public Tips selectProcessDefinitionId(@PathVariable("key") String key) {
        return Tips.getSuccessTips("success", service.selectProcessDefinitionId(key));
    }


}
